//
//  BookMoreVC.m
//  LiteraryTune
//
//  Created by 陈雪丹 on 2018/5/31.
//  Copyright © 2018年 cxd. All rights reserved.
//

#import "BookMoreVC.h"
#import "BookListPageModel.h"
#import "BookCollectCell.h"
#import "BookDetailShowViewController.h"

@interface BookMoreVC () <UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong)UICollectionView *showCollectionView;
//数据源
@property (nonatomic, strong)BookListPageModel *dataModel;

@end

@implementation BookMoreVC
{
    NSInteger pageIndex;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    pageIndex = 1;
    if (self.navigationItem.title == nil) {
        self.navigationItem.title = @"图书";
    }
    [self getNetData];
    [self buildUI];
}

- (void)loadData {
    NSMutableDictionary *dict = @{@"pageIndex": @(pageIndex), @"pageSize": @21}.mutableCopy;
    if (self.classifyId.length > 0) {
        [dict setObject:self.classifyId forKey:@"lm"];
    }
    __weak typeof(self) weakSelf = self;
    [CXDNetHelp getRequestWithURLString:@"http://www.wenqujingdian.com/index.php/jiekou/getBookByPage"
                             parameters:dict
                                success:^(id  _Nullable responseObject) {
                                    
                                    BookListPageModel *model = [BookListPageModel mj_objectWithKeyValues:responseObject];
                                    if (model.list.count < 21) {
                                        [self.showCollectionView.mj_footer endRefreshingWithNoMoreData];
                                    } else {
                                        [self.showCollectionView.mj_footer resetNoMoreData];
                                    }
                                    if (pageIndex == 1) {
                                        self.dataModel = model;
                                    } else {
                                        NSMutableArray *array = self.dataModel.list.mutableCopy;
                                        [array addObjectsFromArray:model.list];
                                        self.dataModel.list = array.copy;
                                    }
                                    [weakSelf.showCollectionView reloadData];
                                } failure:^(NSError * _Nullable error) {
                                    
                                }];
}

- (void)getNetData {
    
    pageIndex = 1;
    [self loadData];
}

- (void)loadMoreData {
    
    pageIndex++;
    [self loadData];
}

- (void)buildUI {
    [self.view addSubview:self.showCollectionView];
    MJRefreshAutoFooter *footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    self.showCollectionView.mj_footer = footer;
}
#pragma mark - 懒加载
- (UICollectionView *)showCollectionView {
    if (!_showCollectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake((kScreenWidth ) / 3, 150);
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        _showCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - CXDTopHeight ) collectionViewLayout:layout];
        _showCollectionView.delegate = self;
        _showCollectionView.dataSource = self;
        _showCollectionView.backgroundColor = RGB(238, 238, 238);
        _showCollectionView.showsVerticalScrollIndicator = NO;
        [_showCollectionView registerNib:[UINib nibWithNibName:NSStringFromClass([BookCollectCell class]) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([BookCollectCell class])];
    }
    return _showCollectionView;
}

#pragma mark - UICollectionViewDelegate,UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataModel.list.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    BookCollectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([BookCollectCell class]) forIndexPath:indexPath];
    cell.backView.hidden = YES;
    BookListPageItemModel *tempModel = self.dataModel.list[indexPath.item];
    [cell.bookImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",RootHomeURL,tempModel.spic]]];
    cell.nameLabel.text = tempModel.news_tit;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (![CXDHelper getUserLoginStatus] && [NSUserDefaults.standardUserDefaults boolForKey:K_NeedLogin]) {
        LoginViewController *loginVC = [[LoginViewController alloc] init];
        [self.navigationController pushViewController:loginVC animated:YES];
        [CXDNoticeView showNoticeViewWithString:@"请先进行登陆"];
        return;
    }
    BookDetailShowViewController *detailVC = [[BookDetailShowViewController alloc] init];
    BookListPageItemModel *tempModel = self.dataModel.list[indexPath.item];
    detailVC.navigationItem.title = tempModel.news_tit;
    detailVC.bookId = tempModel.bookID;
    [self.navigationController pushViewController:detailVC animated:YES];
}


@end
